package org.boot.fast.core.api.controller;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.boot.fast.core.api.service.DynamicSqlService;
import org.boot.fast.dao.entity.FastApiConfigDO;
import org.boot.fast.dao.service.FastApiConfigService;
import org.boot.fast.pojo.CommonResult;
import org.boot.fast.pojo.PageResult;
import org.boot.fast.util.CollUtil;
import org.boot.fast.util.ObjectUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

import static org.boot.fast.pojo.CommonResult.success;

@Tag(name = "动态接口统一控制器")
@RestController
@SuppressWarnings("all")
@RequestMapping("/fast/api")
public class DynamicSqlController {
    
    @Autowired
    private DynamicSqlService dynamicSqlService;
    @Resource
    private FastApiConfigService fastApiConfigService;

    @PostMapping("/i/{version}/{apiKey}")
    @Operation(summary = "通用新增接口(支持新增单条、批量)")
    public CommonResult<?> insert(@PathVariable String version,
                                  @PathVariable String apiKey,
                                  @RequestBody Object params) {
        if (ObjectUtil.isEmpty( params)) {
            throw new RuntimeException("参数不能为空");
        }
        FastApiConfigDO fastApiConfigDO = fastApiConfigService.lambdaQuery()
                .eq(FastApiConfigDO::getVersion, version)
                .eq(FastApiConfigDO::getApiKey, apiKey).one();
        if (fastApiConfigDO == null) {
            throw new RuntimeException("API配置不存在");
        }
        if (fastApiConfigDO.getSqlType() == 1) { //insert
            return success(dynamicSqlService.executeUpdate(fastApiConfigDO, (Map<String, Object>) params));
        } else if (fastApiConfigDO.getSqlType() == 5){
            return success(dynamicSqlService.executeUpdateBatch(fastApiConfigDO, (List<Map<String, Object>>) params));
        }
        throw new RuntimeException("make不支持的api类型!");
    }
    
    @DeleteMapping("/d/{version}/{apiKey}")
    @Operation(summary = "通用删除接口(支持删除单条、批量)")
    public CommonResult<?> delete(@PathVariable String version,
                                  @PathVariable String apiKey,
                                  @RequestParam Map<String, Object> params) {
        if (ObjectUtil.isEmpty( params)) {
            throw new RuntimeException("参数不能为空");
        }
        FastApiConfigDO fastApiConfigDO = fastApiConfigService.lambdaQuery()
                .eq(FastApiConfigDO::getVersion, version)
                .eq(FastApiConfigDO::getApiKey, apiKey).one();
        if (fastApiConfigDO == null) {
            throw new RuntimeException("API配置不存在");
        }
        if (fastApiConfigDO.getSqlType() == 2) { //delete
            return success(dynamicSqlService.executeUpdate(fastApiConfigDO, params));
        }
        throw new RuntimeException("make不支持的api类型!");
    }
    @PutMapping("/u/{version}/{apiKey}")
    @Operation(summary = "通用更新接口(支持更新单条)")
    public CommonResult<?> update(@PathVariable String version,
                                  @PathVariable String apiKey,
                                  @RequestBody Map<String, Object> params) {
        if (ObjectUtil.isEmpty( params)) {
            throw new RuntimeException("参数不能为空");
        }
        FastApiConfigDO fastApiConfigDO = fastApiConfigService.lambdaQuery()
                .eq(FastApiConfigDO::getVersion, version)
                .eq(FastApiConfigDO::getApiKey, apiKey).one();
        if (fastApiConfigDO == null) {
            throw new RuntimeException("API配置不存在");
        }
        if (fastApiConfigDO.getSqlType() == 3) { //update
            return success(dynamicSqlService.executeUpdate(fastApiConfigDO, params));
        }
        throw new RuntimeException("make不支持的api类型!");
    }

    /**
     * 通用查询接口
     */
    @GetMapping("/q/{version}/{apiKey}")
    @Operation(summary = "通用查询接口(支持分页查询、列表查询)")
    public CommonResult<?> query(@PathVariable String version,
                                 @PathVariable String apiKey,
                                 @RequestParam Map<String, Object> params) {
        FastApiConfigDO fastApiConfigDO = fastApiConfigService.lambdaQuery()
                .eq(FastApiConfigDO::getVersion, version)
                .eq(FastApiConfigDO::getApiKey, apiKey).one();
        if (fastApiConfigDO == null) {
            throw new RuntimeException("API配置不存在");
        }
        if (fastApiConfigDO.getSqlType() == 4) { //select
            if (CollUtil.isNotEmpty(params) && params.containsKey("pageNo") && params.containsKey("pageSize")){
                return success(new PageResult<>(
                        dynamicSqlService.executeQuery(fastApiConfigDO, params),
                        dynamicSqlService.executeCountQuery(fastApiConfigDO, params)
                ));
            }else{
                return success(new PageResult<>(
                        dynamicSqlService.executeQuery(fastApiConfigDO, params),
                        -1L
                ));
            }
        }
        throw new RuntimeException("read不支持的api类型!");
    }
}
